package com.cookbook.controller;

import com.cookbook.RequestAuthority;
import com.cookbook.Result;
import com.cookbook.database.model.FavoritesKey;
import com.cookbook.service.FavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by 胡玉斌 on 2017/7/4.
 */

@Controller
@RequestMapping("/favorite")
@SessionAttributes("user")
public class FavoritesController {
    @Autowired
    FavoriteService favoriteService;

    @RequestAuthority
    @Transactional
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public String historyList(
            @RequestParam("userid") Integer userid) {
        if (userid < 0 ) {
            return Result.format(Result.PARAMETER_INVALID, "非法参数");
        }
        List<FavoritesKey> favoritesKeys;
        try {
            favoritesKeys = favoriteService.select(userid);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.format(Result.INTERNAL_ERROR, "内部错误");
        }
        return Result.success(favoritesKeys);
    }

    @RequestAuthority
    @Transactional
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ResponseBody
    public String insert(
            @RequestParam("userid") Integer userid,
            @RequestParam("cookbookid") Integer cookbookid) {
        if (userid < 0 || cookbookid < 0) {
            return Result.format(Result.PARAMETER_INVALID, "非法参数");
        }

        List<FavoritesKey> favoritesKeysExist;
        favoritesKeysExist = favoriteService.select(userid);
        if(!favoritesKeysExist.isEmpty()) return Result.format(Result.PARAMETER_INVALID, "该记录已经存在");

        try {
            favoriteService.insert(userid,cookbookid);
            return Result.format(Result.OK, "添加喜爱成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.format(Result.INTERNAL_ERROR, "内部错误");
        }
    }

    @RequestAuthority
    @Transactional
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @ResponseBody
    public String delete(
            @RequestParam("userid") Integer userid,
            @RequestParam("cookbookid") Integer cookbookid) {
        if (userid < 0 || cookbookid < 0) {
            return Result.format(Result.PARAMETER_INVALID, "非法参数");
        }

        List<FavoritesKey> favoritesKeysExist;
        favoritesKeysExist = favoriteService.select(userid);
        if(favoritesKeysExist.isEmpty()) return Result.format(Result.PARAMETER_INVALID, "该记录不存在");

        try {
            favoriteService.deleteSelfByFavoriteId(userid,cookbookid);
            return Result.format(Result.OK, "删除喜爱成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.format(Result.INTERNAL_ERROR, "内部错误");
        }
    }
}
